package com.ruoyi.wzgl.wzck.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.wzgl.wzck.domain.Wzck;
import com.ruoyi.wzgl.wzck.domain.WzckCkmx;
import com.ruoyi.wzgl.wzck.mapper.WzckMapper;
import com.ruoyi.wzgl.wzck.service.IWzckService;
import com.ruoyi.wzgl.wzck.vo.WzCkTopVo;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 物资出库Service业务层处理
 *
 * @author huacai
 * @date 2024-09-25
 */
@Service
public class WzckServiceImpl implements IWzckService {
    @Resource
    private WzckMapper wzckMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询物资出库
     *
     * @param wzckId 物资出库主键
     * @return 物资出库
     */
    @Override
    public Wzck selectWzckByWzckId(Integer wzckId) {
        return wzckMapper.selectWzckByWzckId(wzckId);
    }

    /**
     * 查询物资出库列表
     *
     * @param wzck 物资出库
     * @return 物资出库
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<Wzck> selectWzckList(Wzck wzck) {
        return wzckMapper.selectWzckList(wzck);
    }

    /**
     * 新增物资出库
     *
     * @param wzck 物资出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWzck(Wzck wzck) {
        wzck.setCreateTime(DateUtils.getNowDate());
        int rows = wzckMapper.insertWzck(wzck);
        insertWzckCkmx(wzck);
        return rows;
    }

    /**
     * 报废出库
     * @param wzck
     * @return
     */
    @Override
    public int insertBfCk(Wzck wzck) {
        wzck.setCreateTime(DateUtils.getNowDate());
        int rows = wzckMapper.insertBfCk(wzck);
        insertWzckCkmx(wzck);
        return rows;
    }

    /**
     * 个人领用出库
     * @param wzck
     * @return
     */
    @Override
    public int insertGrLyCk(Wzck wzck) {
        wzck.setCreateTime(DateUtils.getNowDate());
        int rows = wzckMapper.insertGrLyCk(wzck);
        insertWzckCkmx(wzck);
        return rows;
    }

    /**
     * 批量新增物资出库
     *
     * @param wzcks 物资出库List
     * @return 结果
     */
    @Override
    public int batchInsertWzck(List<Wzck> wzcks) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(wzcks)) {
            try {
                for (int i = 0; i < wzcks.size(); i++) {
                    int row = wzckMapper.insertWzck(wzcks.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == wzcks.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改物资出库
     *
     * @param wzck 物资出库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWzck(Wzck wzck) {
        wzck.setUpdateTime(DateUtils.getNowDate());
        wzckMapper.deleteWzckCkmxByWzckId(wzck.getWzckId());
        insertWzckCkmx(wzck);
        return wzckMapper.updateWzck(wzck);
    }

    /**
     * 确定出库
     * @param wzck
     * @return
     */
    @Override
    @Transactional
    public int updateQdck(Wzck wzck) {
        return wzckMapper.updateQdck(wzck);
    }

    /**
     * 批量删除物资出库
     *
     * @param wzckIds 需要删除的物资出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzckByWzckIds(Integer[] wzckIds) {
        wzckMapper.deleteWzckCkmxByWzckIds(wzckIds);
        return wzckMapper.deleteWzckByWzckIds(wzckIds);
    }

    /**
     * 删除物资出库信息
     *
     * @param wzckId 物资出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWzckByWzckId(Integer wzckId) {
        wzckMapper.deleteWzckCkmxByWzckId(wzckId);
        return wzckMapper.deleteWzckByWzckId(wzckId);
    }

    /**
     * 一级库物资出库Top10
     * @return
     */
    @Override
    public List<WzCkTopVo> getWzCkTop() {
        return wzckMapper.getWzCkTop();
    }

    /**
     * 新增出库明细信息
     *
     * @param wzck 物资出库对象
     */
    public void insertWzckCkmx(Wzck wzck) {
        List<WzckCkmx> wzckCkmxList = wzck.getWzckCkmxList();
        Integer wzckId = wzck.getWzckId();
        if (StringUtils.isNotNull(wzckCkmxList)) {
            List<WzckCkmx> list = new ArrayList<WzckCkmx>();
            for (WzckCkmx wzckCkmx : wzckCkmxList) {
                wzckCkmx.setWzckId(wzckId);
                list.add(wzckCkmx);
            }
            if (list.size() > 0) {
                wzckMapper.batchWzckCkmx(list);
            }
        }
    }
}
